// 模拟数据生成脚本

// 初始化模拟数据
function initializeMockData() {
    console.log("正在初始化模拟数据...");
    
    // 检查是否已有数据
    const storageManager = new window.AppData.StorageManager();
    const data = storageManager.getData();
    
    // 如果已有交易数据和储蓄目标数据，则不生成模拟数据
    if (data.transactions && data.transactions.length > 0 && 
        data.savingsGoals && data.savingsGoals.length > 0) {
        console.log("已有数据，不生成模拟数据");
        return;
    }
    
    // 生成模拟交易数据
    const mockTransactions = generateMockTransactions();
    
    // 生成模拟储蓄目标数据
    const mockSavingsGoals = generateMockSavingsGoals();
    
    // 更新数据
    data.transactions = mockTransactions;
    data.savingsGoals = mockSavingsGoals;
    
    // 保存数据
    storageManager.saveData(data);
    
    console.log("模拟数据生成完成");
}

// 生成模拟交易数据
function generateMockTransactions() {
    const transactions = [];
    const expenseCategories = window.AppData.expenseCategories;
    const incomeCategories = window.AppData.incomeCategories;
    
    // 获取当前日期
    const currentDate = new Date();
    const currentYear = currentDate.getFullYear();
    const currentMonth = currentDate.getMonth();
    
    // 生成最近3个月的交易数据
    for (let m = 0; m < 3; m++) {
        const month = currentMonth - m;
        const year = month < 0 ? currentYear - 1 : currentYear;
        const adjustedMonth = month < 0 ? month + 12 : month;
        
        // 每月收入：工资和其他收入
        const salaryDate = new Date(year, adjustedMonth, 10);
        const salaryCategory = incomeCategories.find(c => c.name === "工资") || incomeCategories[0];
        transactions.push({
            id: `mock-income-salary-${year}-${adjustedMonth}`,
            type: 'income',
            amount: 8000 + Math.floor(Math.random() * 2000),
            date: salaryDate.toISOString(),
            categoryId: salaryCategory.id,
            note: "月度工资"
        });
        
        // 可能的额外收入
        if (Math.random() > 0.5) {
            const extraIncomeDate = new Date(year, adjustedMonth, 20 + Math.floor(Math.random() * 8));
            const extraCategory = incomeCategories.find(c => c.name !== "工资") || incomeCategories[1] || incomeCategories[0];
            transactions.push({
                id: `mock-income-extra-${year}-${adjustedMonth}`,
                type: 'income',
                amount: 500 + Math.floor(Math.random() * 3000),
                date: extraIncomeDate.toISOString(),
                categoryId: extraCategory.id,
                note: "额外收入"
            });
        }
        
        // 每月支出：10-15笔
        const transactionsCount = 10 + Math.floor(Math.random() * 6);
        for (let i = 0; i < transactionsCount; i++) {
            const day = 1 + Math.floor(Math.random() * 28);
            const date = new Date(year, adjustedMonth, day);
            const categoryIndex = Math.floor(Math.random() * expenseCategories.length);
            const amount = 10 + Math.floor(Math.random() * 1000);
            
            transactions.push({
                id: `mock-expense-${year}-${adjustedMonth}-${i}`,
                type: 'expense',
                amount: amount,
                date: date.toISOString(),
                categoryId: expenseCategories[categoryIndex].id,
                note: `${expenseCategories[categoryIndex].name}支出`
            });
        }
        
        // 固定支出：房租
        const rentDate = new Date(year, adjustedMonth, 5);
        const rentCategory = expenseCategories.find(c => c.name === "住房") || expenseCategories.find(c => c.icon === "home") || expenseCategories[0];
        transactions.push({
            id: `mock-expense-rent-${year}-${adjustedMonth}`,
            type: 'expense',
            amount: 2000,
            date: rentDate.toISOString(),
            categoryId: rentCategory.id,
            note: "房租"
        });
    }
    
    // 按日期排序（从新到旧）
    transactions.sort((a, b) => new Date(b.date) - new Date(a.date));
    
    return transactions;
}

// 生成模拟储蓄目标数据
function generateMockSavingsGoals() {
    return [
        {
            id: "mock-savings-goal-1",
            name: "旅行基金",
            targetAmount: 10000,
            savingRate: 10,
            type: "short",
            icon: "flight_takeoff"
        },
        {
            id: "mock-savings-goal-2",
            name: "购车",
            targetAmount: 100000,
            savingRate: 20,
            type: "medium",
            icon: "directions_car"
        },
        {
            id: "mock-savings-goal-3",
            name: "购房首付",
            targetAmount: 300000,
            savingRate: 30,
            type: "long",
            icon: "home"
        }
    ];
}

// 导出模块
window.MockData = {
    initializeMockData
}; 